import javax.swing.*; import java.awt.*; import java.awt.event.*; import java.net.*; import java.io.*; class Game2 extends JPanel implements MouseListener, ActionListener, Runnable { BufferedReader sisse; PrintWriter valja; static JFrame frame; static int korgus = 22, laius = 22, rida = 19, veerg = 19; Button reset = new Button("Uus mäng"); Button connect = new Button("Ühenda"); Color color; Panel tekstipaneel = new Panel(new GridLayout()); Panel nupupaneel = new Panel(new GridLayout()); TextField tekst = new TextField(); boolean clean = false, won = false, connected = false; String users_c[] = {"sinine", "punane"}; int x, y, user, mx = 0, my = 0, w, users[] = {0, 1}, field[][] = new int[rida][veerg], winner, ax = 0, ay = 0, bx = 0, by = 0; boolean paintball = false; Panel mangupaneel = new Panel() { public void paint(Graphics g) { for(int i = 0; i < rida; i++) { for(int j = 0; j < veerg; j++) { g.setColor(Color.white); g.drawRect(i * laius, j * korgus, laius, korgus); w = 2; if(clean) { field[i][j] = 2; } else { w = field[i][j]; } g.setColor(colors[w]); g.fillOval((i * laius) + 3, (j * korgus) + 3, laius - 6, korgus - 6); } } if(won) { g.setColor(Color.white); g.drawLine(ax * laius + 12, ay * korgus + 12, bx * laius + 12, by * korgus + 12); } clean = false; } }; Color[] colors = {Color.blue, Color.red, getBackground()}; public Game2() { user = users[(int)Math.random()]; tekst.setEnabled(false); tekst.setText(users_c[user]+" mängib"); setLayout(new BorderLayout()); nupupaneel.setLayout(new BorderLayout()); nupupaneel.add(reset, BorderLayout.NORTH); nupupaneel.add(connect, BorderLayout.SOUTH); tekstipaneel.add(tekst); add(tekstipaneel, BorderLayout.NORTH); add(nupupaneel, BorderLayout.SOUTH); add(mangupaneel, BorderLayout.CENTER); nupp1.addActionListener(this); mangupaneel.addMouseListener(this); clean = true; mangupaneel.repaint(); } public void actionPerformed(ActionEvent a) { if(a.getSource() == reset) { clean = true; won = false; tekst.setText(users_c[user]+" mängib"); mangupaneel.repaint(); } elseif(a.getSource() == connect) { if(!connected) { Socket sc = new Socket("ws0.cs1.tpu.ee", 3001); sisse = new BufferedReader(new InputStreamReader(sc.getInputStream())); valja = new PrintWriter(sc.getOutputStream(), true); new Thread(this).start(); } else { valja.println("end"); } } } public void checkForWinner() { for(int z = 0; z < users.length; z++) { for(int i = 0; i < rida; i++) { for(int j = 0; j < veerg; j++) { if(field[i][j] == z) { if(j - 4 > -1 && i + 4 < rida) { if(field[i+1][j-1] == z && field[i+2][j-2] == z && field[i+3][j-3] == z && field[i+4][j-4] == z) { ax = i; ay = j; bx = i + 4; by = j - 4; winner = z; won = true; } } if(j + 4 < veerg && i + 4 < rida) { if(field[i+1][j+1] == z && field[i+2][j+2] == z && field[i+3][j+3] == z && field[i+4][j+4] == z) { ax = i; ay = j; bx = i + 4; by = j + 4; winner = z; won = true; } } if(j + 4 < veerg) { if(field[i][j+1] == z && field[i][j+2] == z && field[i][j+3] == z && field[i][j+4] == z) { ax = i; ay = j; bx = i; by = j + 4; winner = z; won = true; } } if(i + 4 < rida) { if(field[i+1][j] == z && field[i+2][j] == z && field[i+3][j] == z && field[i+4][j] == z) { ax = i; ay = j; bx = i + 4; by = j; winner = z; won = true; } } } } } } if(won) { //clean = true; tekst.setText(users_c[winner]+" võitis!"); } } public void mousePressed(MouseEvent e) { x = e.getX(); y = e.getY(); for(int i = 0; i < rida; i++) { for(int j = 0; j < veerg; j++) { if(x > i * laius && x < (i * laius + laius) && y > j * korgus && y < (j * korgus + laius) && field[i][j] == 2 && !won) { field[i][j] = user; user = user == users[0] ? users[1] : users[0]; tekst.setText(users_c[user]+" mängib"); } } } mangupaneel.repaint(); checkForWinner(); } public void mouseEntered(MouseEvent e) { } public void mouseExited(MouseEvent e) { } public void mouseClicked(MouseEvent e) { } public void mouseReleased(MouseEvent e) { } public void run() { try { // siia loeb sisse asju, mida server saadab } catch(Exception ex) { ex.printStackTrace(); } } public static void main(String[] args) { frame = new JFrame("VIIS RITTA (beta)"); frame.setSize(427, 500); frame.getContentPane().setBackground(new Color(240, 245, 250)); frame.getContentPane().add(new Game2()); frame.setVisible(true); frame.setResizable(false); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); } }